chore: add cache hit/miss instrumentation to pagination total cache#2443
Conversation
Reviewer's guide (collapsed on small PRs)Reviewer's GuideAdds tracing instrumentation to PaginationCache::cached_total to record cache hits vs misses for pagination total caching, wiring the cache key into the span and toggling a cache_hit field based on whether the cache initialization closure runs. Sequence diagram for cache hit/miss instrumentation in PaginationCache::cached_totalsequenceDiagram
actor Caller
participant PaginationCache
participant Cache
participant tracing
Caller->>PaginationCache: cached_total(key, compute)
PaginationCache->>tracing: span with field cache_hit = true
PaginationCache->>Cache: try_get_with(key, compute)
alt cache miss (init closure runs)
Cache->>tracing: Span::current().record(cache_hit, false)
Cache->>PaginationCache: compute().await result
else cache hit (value already cached)
Cache-->>PaginationCache: cached result (cache_hit remains true)
end
PaginationCache-->>Caller: total_count
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
5b4b7e7 to
ce4c8bb
Compare
There was a problem hiding this comment.
Hey - I've found 1 issue
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="common/src/db/pagination_cache.rs" line_range="72" />
<code_context>
self.cache
.try_get_with(key, async {
misses.add(1, &[]);
+ tracing::Span::current().record("cache_hit", false);
compute().await
})
</code_context>
<issue_to_address>
**suggestion (bug_risk):** Be cautious relying on `Span::current()` inside the `try_get_with` async closure.
Because `try_get_with` controls how the async closure is scheduled, it may run on a different task where the `#[instrument]` span is no longer current, making this `record` call a no-op or updating the wrong span. If you need reliable instrumentation, capture the span at the start of `cached_total` (e.g. `let span = tracing::Span::current();`) and call `span.record("cache_hit", false);` inside the closure instead of using `Span::current()` there.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| self.cache | ||
| .try_get_with(key, async { | ||
| misses.add(1, &[]); | ||
| tracing::Span::current().record("cache_hit", false); |
There was a problem hiding this comment.
suggestion (bug_risk): Be cautious relying on Span::current() inside the try_get_with async closure.
Because try_get_with controls how the async closure is scheduled, it may run on a different task where the #[instrument] span is no longer current, making this record call a no-op or updating the wrong span. If you need reliable instrumentation, capture the span at the start of cached_total (e.g. let span = tracing::Span::current();) and call span.record("cache_hit", false); inside the closure instead of using Span::current() there.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2443 +/- ##
==========================================
+ Coverage 71.39% 71.41% +0.01%
==========================================
Files 453 453
Lines 27254 27251 -3
Branches 27254 27251 -3
==========================================
+ Hits 19459 19460 +1
+ Misses 6666 6657 -9
- Partials 1129 1134 +5 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
/backport |
|
Successfully created backport PR for |
Summary
#[instrument]toPaginationCache::cached_totalwith the cache key and acache_hitfieldcache_hit = true; the init closure sets it tofalsewhen actually called (cache miss)Test plan
cache_hitfield appears in traces for paginated list endpointscache_hit=false, subsequent identical requests showcache_hit=true🤖 Generated with Claude Code
Summary by Sourcery
Enhancements: